package com.pipayshop.controller;

import com.pipayshop.entity.dto.HotelLoginDTO;
import com.pipayshop.entity.dto.HotelShopInfoListByConditionDTO;
import com.pipayshop.entity.dto.HotelUserAdminDTO;
import com.pipayshop.entity.vo.*;
import com.pipayshop.exception.BusinessException;
import com.pipayshop.service.HotelShopInfoService;
import com.pipayshop.service.HotelShopOrderInfoService;
import com.pipayshop.service.HotelUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 实体店酒店的信息 前端控制器
 * </p>
 *
 * @author nws
 * @since 2023-07-25
 */
@Api(value = "酒店管理端接口",tags = "酒店管理端接口")
@RestController
@RequestMapping("/pipayshopapi/hotel_manger")
@Slf4j
public class HotelMangerController {
    @Resource
    private HotelShopInfoService infoService;
    @Resource
    private HotelUserInfoService userInfoService;
    @Resource
    private HotelShopOrderInfoService orderInfoService;


    @PostMapping("login")
    @Operation(summary = "登录")
    public ResponseVO<ResponseResultVO> login(@RequestBody HotelLoginDTO hotelLoginDTO) {
        try {
            ResponseResultVO responseResultVO = userInfoService.login(hotelLoginDTO);
            return ResponseVO.getSuccessResponseVo(responseResultVO);
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("login_failed_contact_admin");
        }
    }

    @PostMapping("register")
    @Operation(summary = "入驻")
    public ResponseVO<ResponseResultVO> register(@RequestBody HotelUserAdminDTO hotelUserAdminDTO) {
        try {
            ResponseResultVO responseResultVO = userInfoService.register(hotelUserAdminDTO);
            return ResponseVO.getSuccessResponseVo(responseResultVO);
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("registration_failed_contact_admin");
        }
    }
    // 根据订单id删除酒店订单
    @PostMapping("deleteOrder/{orderId}")
    @ApiOperation("删除酒店订单接口")
    public ResponseVO<Boolean> deleteOrderByOrderId(@PathVariable String orderId) {
        try {
            boolean result = orderInfoService.deleteOrderByOrderId(orderId);
            return ResponseVO.getSuccessResponseVo(result);
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("delete_hotel_order_failed_contact_admin");
        }
    }

    // 更新酒店信息
    @PostMapping("updateHotel")
    @ApiOperation("更新酒店信息")
    public ResponseVO<Boolean> updateHotel(@RequestBody ShopInfoVO shopInfoVO) {
        try {
            boolean result = infoService.updateHotel(shopInfoVO);
            return ResponseVO.getSuccessResponseVo(result);
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("update_hotel_info_failed_contact_admin");
        }
    }

    @GetMapping("getShopInfoByUid/{uid}")
    @ApiOperation("根据uid查询酒店详情")
    public ResponseVO getShopInfoByUid(@PathVariable String uid) {
        try {
            HotelShopInfoVO shopByIdVO = infoService.getShopInfoByUid(uid);
            return ResponseVO.getSuccessResponseVo(shopByIdVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("query_hotel_details_by_uid_failed_contact_admin");
        }
    }

    @GetMapping("getShopInfoByShopId/{shopId}")
    @ApiOperation("根据酒店id查询酒店房间信息")
    public ResponseVO getShopInfoByShopId(@PathVariable String shopId) {
        try {
            List<RoomListVO> shopInfoVO = infoService.getShopInfoByShopId(shopId);
            return ResponseVO.getSuccessResponseVo(shopInfoVO);
        }catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("query_hotel_rooms_by_id_failed_contact_admin");
        }
    }

    // 查询所有酒店订单
    @GetMapping("getAllOrderByShopId/{shopId}")
    @ApiOperation("根据uid查询所有酒店订单")
    public ResponseVO getAllOrderByShopId(@PathVariable String shopId) {
        try {
            List<HotelStoreOrderInfoVO> shopInfoVO = orderInfoService.getAllOrderByShopId(shopId);
            return ResponseVO.getSuccessResponseVo(shopInfoVO);
        }catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("retrieve_hotel_room_info_failed_contact_staff");
        }
    }

    // 上下架酒店房间
    @PostMapping("takeOffRoom/{roomId}/{hotelId}")
    @ApiOperation("上下架酒店房间")
    public ResponseVO takeOffRoom(@PathVariable String roomId, @PathVariable String hotelId) {
        try {
            boolean result = infoService.takeOffRoom(roomId,hotelId);
            return ResponseVO.getSuccessResponseVo(result);
        }catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("hotel_room_toggle_failed_contact_staff");
        }
    }
}
